package com.nd.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nd.common.result.ResultUtils;
import com.nd.common.utils.EmptyUtils;
import com.nd.entity.Cart;
import com.nd.entity.Product;
import com.nd.service.ICartService;
import com.nd.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhaoyan
 * @since 2024-12-25
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    ICartService service;

    /**
     * 添加
     * @param t
     * @return
     */
//    @PostMapping("/")
//    public Object add(@RequestBody Cart t){
//        if(service.save(t)){
//            return ResultUtils.returnSuccess();
//        }else{
//            return ResultUtils.returnFail("添加失败");
//        }
//    }

    @PostMapping("/")
    public Object add(@RequestBody Cart t){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("desk_id",t.getDeskId());
        queryWrapper.eq("product_id",t.getProductId());

        //若对象不为空--查到了---修改数量，+1
        Cart one = service.getOne(queryWrapper);
        if(EmptyUtils.isNotEmpty(one)){
            //修改数量(在原来的基础上+1)
            one.setNum(one.getNum()+1);
            if(service.updateById(one)){
                return ResultUtils.returnSuccess();
            }else{
                return ResultUtils.returnFail("修改失败");
            }

        }else{ //没查到，新增
            //增加一条数据
            if(service.save(t)){
                return ResultUtils.returnSuccess();
            }else{
                return ResultUtils.returnFail("添加失败");
            }
        }
    }




}
